﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.PhanCongGiangDayService;
namespace eTMS.Controllers
{
    public class PhanCongGiangDayControllerBase : CommonController
    {
        public PhanCongGiangDayControllerBase()
            : base()
        {
            this.dao = new PhanCongGiangDayServiceClient();
        }
        ~PhanCongGiangDayControllerBase()
        {
            dao.Close();
        }
        private DataTable _Table;
        private PhanCongGiangDayEntity oPhanCongGiangDay;
        public PhanCongGiangDayEntity PhanCongGiangDayObject
        {
            get
            {
                return oPhanCongGiangDay;
            }
            set
            {
                oPhanCongGiangDay = value;
            }
        }
        protected PhanCongGiangDayServiceClient dao;
        public DataTable Table
        {
            get
            {
                this.LoadAllDSPhanCongGiangDay(false);
                return _Table;
            }
        }
        public void LoadAllDSPhanCongWithGiaoVienMonHoc(bool clearCache)
        {
            bool bForce = clearCache;
            if (_Table == null)
            {
                bForce = true;
            }
            if (!bForce) return;
            DataSet ds = dao.SelectAllDSWithMonHocByGiangVien(0);
            if (ds.Tables.Count > 0)
            {
                _Table = ds.Tables[0];
                this.AddIndexToTable(_Table);
            }
        }
        public void LoadAllDSPhanCongGiangDay(bool clearCache)
        {
            bool bForce = clearCache;
            if (_Table == null)
            {
                bForce = true;
            }
            if (!bForce) return;
            DataSet ds = dao.SelectAllDS();
            if (ds.Tables.Count > 0)
            {
                _Table = ds.Tables[0];
                this.AddIndexToTable(_Table);
            }
        }
        public void AddIndexToTable()
        {
            this.AddIndexToTable(_Table);
        }
        public void ResetTableIndex()
        {
            this.ResetTableIndex(_Table);
        }
        public bool AddNew()
        {
            if (oPhanCongGiangDay == null) return false;
            bool result = true;
            try
            {
                oPhanCongGiangDay = dao.SaveRet(oPhanCongGiangDay);
            }
            catch
            {
                result = false;
            }
            if (result)
            {
                _ListPhanCongGiangDay.Add(oPhanCongGiangDay);
            }
            return result;
        }
        public bool Update()
        {
            if (oPhanCongGiangDay == null) return false;
            return dao.Update(oPhanCongGiangDay);
        }
        public bool Delete()
        {
            if (oPhanCongGiangDay == null) return false;
            bool retvalue = dao.Delete(oPhanCongGiangDay);
            if (retvalue)
            {
                _ListPhanCongGiangDay.Remove(oPhanCongGiangDay);
            }
            return retvalue;
        }
        public bool DeleteByID(Int32 PhanCongGiangDay_ID)
        {
            bool retvalue = dao.DeleteByID(PhanCongGiangDay_ID);
            if (retvalue)
            {
                PhanCongGiangDayEntity oDeleted = FindPhanCongGiangDay(PhanCongGiangDay_ID);
                if (oDeleted != null)
                {
                    _ListPhanCongGiangDay.Remove(oDeleted);
                }
            }
            return retvalue;
        }
        public void ResetObject()
        {
            this.oPhanCongGiangDay = new PhanCongGiangDayEntity();
        }
        //Copy data from data row in grid to object
        public PhanCongGiangDayEntity MapDataRowToObject(DataRow row)
        {
            PhanCongGiangDayEntity o = new PhanCongGiangDayEntity();
            if (!row.IsNull("SucGiang"))
                o.SucGiang = ConvertInt(row["SucGiang"]);
            if (!row.IsNull("GiaoVien_ID"))
                o.GiaoVien_ID = (Int32)(row["GiaoVien_ID"]);
            if (!row.IsNull("MonHoc_ID"))
                o.MonHoc_ID = (Int32)(row["MonHoc_ID"]);
            if (!row.IsNull("PhanCongGiangDay_ID"))
                o.PhanCongGiangDay_ID = (Int32)(row["PhanCongGiangDay_ID"]);
            return o;
        }
        //Copy data from object to data row in grid    
        public void MapObjectToDataRow(PhanCongGiangDayEntity o, DataRow row)
        {
            row["SucGiang"] = o.SucGiang;
            row["GiaoVien_ID"] = o.GiaoVien_ID;
            row["MonHoc_ID"] = o.MonHoc_ID;
            row["PhanCongGiangDay_ID"] = o.PhanCongGiangDay_ID;
        }
        public void MapObjectToDataRow(DataRow row)
        {
            this.MapObjectToDataRow(this.PhanCongGiangDayObject, row);
        }
        public void LoadObject(Int32 PhanCongGiangDay_ID)
        {
            PhanCongGiangDayEntity oLoad = FindPhanCongGiangDay(PhanCongGiangDay_ID);
            if (oLoad == null)
            {
                oPhanCongGiangDay = this.LoadPhanCongGiangDay(PhanCongGiangDay_ID);
                bool bNotIsNull = true;
                bNotIsNull &= oPhanCongGiangDay.IsNullPhanCongGiangDay_ID;
                if (!bNotIsNull)
                {
                    this.ListPhanCongGiangDay.Add(oPhanCongGiangDay);
                }
            }
            else
            {
                oPhanCongGiangDay = oLoad;
            }
        }
        #region CachedList PhanCongGiangDay
        private static List<PhanCongGiangDayEntity> _ListPhanCongGiangDay = new List<PhanCongGiangDayEntity>();
        public List<PhanCongGiangDayEntity> ListPhanCongGiangDay
        {
            get
            {
                return _ListPhanCongGiangDay;
            }
        }
        public static List<PhanCongGiangDayEntity> ListPhanCongGiangDayStatic
        {
            get
            {
                return _ListPhanCongGiangDay;
            }
        }
        public PhanCongGiangDayEntity FindPhanCongGiangDay(Int32 PhanCongGiangDay_ID)
        {
            PhanCongGiangDayEntity oResult = this.ListPhanCongGiangDay.Find(delegate(PhanCongGiangDayEntity oFind)
            {
                bool result = true;
                result &= oFind.PhanCongGiangDay_ID.Equals(PhanCongGiangDay_ID);
                return result;
            });
            return oResult;
        }
        public static void LoadAllPhanCongGiangDay(bool clearCache)
        {
            if (_ListPhanCongGiangDay.Count == 0)
                clearCache = true;
            if (!clearCache) return;
            PhanCongGiangDayServiceClient dao = new PhanCongGiangDayServiceClient();
            _ListPhanCongGiangDay = new List<PhanCongGiangDayEntity>(dao.SelectAll());
            dao.Close();
        }
        public PhanCongGiangDayEntity LoadPhanCongGiangDay(Int32 PhanCongGiangDay_ID)
        {
            PhanCongGiangDayEntity o = dao.SelectOne(PhanCongGiangDay_ID);
            return o;
        }
        #endregion
    }
}
